returntype FunctionName(datatype args1,datatype args2,...) { Statements --------------- return value ; }
class demo { public static void main(String args[]) { System.out.println("CCIT"); star(4); System.out.println("\nAmravati"); star(8); } static void star(int n) { for(int i=1;i<=n;i++) System.out.print("*"); } }
CCIT **** Amravati ********
class demo { public static void main(String args[]) { even(10); System.out.println(); even(15); } static void even(int n) { for(int i=2;i<=n;i+=2) System.out.print(i+" "); } }
2 4 6 8 10 2 4 6 8 10 12 14
class demo { public static void main(String args[]) { display(10,20); System.out.println(); display(5,12); } static void display(int a,int b) { for(int i=a;i<=b;i++) System.out.print(i+" "); } }
10 11 12 13 14 15 16 17 18 19 20 5 6 7 8 9 10 11 12 13
class demo { public static void main(String args[]) { max(10,20); System.out.println(); max(512,105); } static void max(int a,int b) { if(a>b) System.out.println(“Greatest Number is" +a); else System.out.println(“Greatest Number is" +b); } }
Greatest Number is 20 Greatest Number is 512
class demo { public static void main(String args[]) { interest(5000,10.25,3); System.out.println(); interest(20000,9.75,1); } static void interest(int P,double R,int N) { double si = P * R * N / 100.0; System.out.println("Simple Interest is"+si); } }
Simple Interest is 1537.50 Simple Interest is 1950.00
class demo { public static void main(String args[]) { volume(5.6,4.3,3.1); System.out.println(); volume(2,5,3); System.out.println(); volume(2.5,7.5f,3); } static void volume(double L,double B,double H) { double v=L*B*H; System.out.println("volume of box is"+v); } }
volume of box is 74.648 volume of box is 30 volume of box is 56.25
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter Radius :"" ); double n = stdin.nextDouble( ); volume(n); } static void volume(double R) { double v=4/3.0*3.14*R*R*R; System.out.println("volume of sphere is"+v); } }
Enter Radius : 4 volume of sphere is 267.87
import java.util.Scanner; class demo { public static void main(String args[]) { Scanner stdin=new Scanner(System.in); System.out.println("Enter Radius :"); double n=stdin.nextDouble(); area(n); circumference(n); } static void area(double R) { double a=3.14*R*R; System.out.println("Area of circle is "+a); } static void circumference(double R) { double c =2*3.14*R ; System.out.println("Circumference of circle is "+c); } }
Enter Radius : 5 Area of circle is 78.5 Circumference of circle is 31.4
class demo { public static void main(String args[]) { sum(10); System.out.println(); sum(5); } static void sum(int n) { int s=0; for(int i=1;i<=n;i++) s=s+i; System.out.println(" Sum is "+ s ); } }
Sum is 55 Sum is 15
class demo { public static void main(String args[]) { fact(5); System.out.println( ); fact(7); } static void fact (int n) { int s=1; for(int i=1;i<=n;i++) s=s*i; System.out.println("Factorial is "+s); } }
Factorial is 120 Factorial is 5040